import fse from "fs-extra";
import path from "node:path";
import archiver from "archiver";
export const saveChunk = (savePath, oldPath, chunkHash) => {
  if (!fse.existsSync(savePath)) {
    fse.mkdirSync(savePath);
  }
  try {
    return fse.moveSync(oldPath, path.join(savePath, chunkHash));
  } catch (err) {
    return err;
  }
};

export const getImageType = (fileName) => {
  let type = path.extname(fileName).toLowerCase().slice(1);
  if (type === "jpg") {
    type = "jpeg";
  }
  return type;
};

export const zipFiles = (zipFileList) => {
  const archive = archiver("zip", { zlib: { level: 9 } });
  for (const zipFile of zipFileList) {
    archive.file(zipFile.filePath, { name: zipFile.fileName });
  }
  return archive;
};

export const zipDirectory = (dirPath) => {
  const archive = archiver("zip", { zlib: { level: 9 } });
  archive.directory(dirPath, false);
  return archive;
};
